#define _CRT_SECURE_NO_WARNINGS  1
#include"String.h"

namespace zy
{

	size_t string::find(const char ch)
	{
		for (size_t i = 0; i < _size; i++)
		{
			if (_arr[i] == ch)
				return i;
		}
		return -1;
	}

	size_t string::find(const char* str,size_t pos)
	{
		assert(pos > -1 && pos < _size);
		char* ch = strstr(_arr+pos, str);
		if (ch == nullptr)
			return -1;
		return ch - str;

	}

	string string::substr(size_t pos, size_t len)
	{	
		assert(pos < _size);
		string s;
		if (len + pos > _size)
			len = _size;
		for (size_t i = 0; i < len; i++)
		{
			s += _arr[pos + i];
		}
		return s;

	}




	void string::insert(size_t pos, char ch)
	{
		assert(pos >= 0 && pos <= _size);
		reserve(_size + 1);
		int end = _size + 1;
		while (end >pos)
		{
			_arr[end] = _arr[end - 1];
			end--;
		}
		_arr[pos] = ch;
		char* str = new char[10];
		strcpy(str, _arr);
		string s(str);
	}
	void string::insert(size_t  pos, const char* str)
	{
		assert(pos >= 0 && pos <= _size);
		size_t len = strlen(str);
		if (!len)
			return;
		reserve(_size + len);
		int end = _size + len;
		while (end > pos + len -1)
		{
			_arr[end] = _arr[end - len];
			end--;
		}
		for (int i = 0; i < len; i++)
		{
			_arr[pos + i] = str[i];
		}
		_size = _size + len;
	}
	void string::erase(size_t pos, size_t len)
	{
		if (pos + len >= _size)
		{
			_arr[pos] = '\0';
			_size = pos;
		}
		else
		{
			int end = pos+len;
			while (end<=_size)
			{
				_arr[end - len] = _arr[end];
				end++;
			}
			_size = _size - len;
		}

	}

	void string::reserve(size_t n)
	{
		if (n > _capacity)
		{
			char* str = new char[n + 1];
			strcpy(str, _arr);
			delete _arr;
			_arr = str;
			_capacity = n;
		}
	}

	void string::append(const char* str)
	{
		int len = strlen(str) + _size;
		if (len > _capacity)
			reserve(_capacity ? 2 * _capacity : 4);
		strcpy(_arr + _size, str);
		_size = len;
	}
	
	string& string::operator+=(const char* str)
	{
		append(str);
		return *this;
	}



	void string::push_back(char ch)
	{
		if (_size == _capacity)
			reserve(_capacity ? 2 * _capacity : 4);
		_arr[_size++] = ch;
		_arr[_size] = '\0';
	}


	string& string::operator+=(char ch)
	{
		push_back(ch);
		return *this;
	}

	bool operator>(const string& s1, const string& s2)
	{
		return strcmp(s1.c_str(), s2.c_str()) > 0;
	}

	bool operator==(const string& s1, const string& s2)
	{
		return  strcmp(s1.c_str(), s2.c_str()) == 0;
	}
	bool operator>=(const string& s1, const string& s2)
	{
		return s1 > s2 || s1 == s2;
	}
	bool operator<(const string& s1, const string& s2)
	{
		return !(s1 >= s2);
	}
	bool operator<=(const string& s1, const string& s2)
	{
		return !(s1 > s2);
	}
	bool operator!=(const string& s1, const string& s2)
	{
		return strcmp(s1.c_str(), s2.c_str()) == 0;
	}

	ostream& operator<<(ostream& out, const string& s)
	{
		out << s.c_str();
		return out;
	}
	istream& operator<<(istream& in, string& s)
	{
		s.clear();
		char ch;
		ch = in.get();

		while (ch!=' '&&ch!='\n')
		{
			s += ch;
			ch = in.get();

		}
		return in;
	}
	istream& getline(istream& in, string& str)
	{
		str.clear();
		char ch;
		ch = in.get();
		while ( ch != '\n')
		{
			str += ch;
			ch = in.get();

		}
		return in;
	}
	void string::clear()
	{
		_arr[0] = '\0';
		_size = 0;
	}


}